<?php

class SearchController extends Zend_Controller_Action
{
	//return records no more than 4
    private static $_HAC_RETURN_LIMIT=4;
    
    public function init()
    {
        /* Initialize action controller here */
        //init the ZendX JQuery Helper
//        $this->view->addHelperPath(
//                'ZendX/JQuery/View/Helper/',
//                   'ZendX_JQuery_View_Helper');
//                // action body
//        $emt=new ZendX_JQuery_Form_Element_AutoComplete('ac',array('label' => 'Search'));
//        $emt->setJQueryParam('source','/search/hac');
//        $this->view->autocompleteElement=$emt;
    }

    public function indexAction()
    {
        // action body
        
    }

    /**
     * action for AutoComplete box in the Header.
     * URL looks like host/search/hac?term=ABC
     */
    public function hacAction(){
        //get the request and para
        $phrase=$this->_getParam('term');
        $model_ac= new Model_ACSearch();
        $limit=$this->_HAC_RETURN_LIMIT;
        
        //find users by phrase.
        $ac_results=$model_ac->findByPartial($phrase,$limit);
        $names = array();
        foreach ($ac_results as $user){
        	$names[] = $user->getName();
        }
        //return name list to view with json.
        $this->_helper->json(array_values($names));
    }

    public function vacAction(){
    	
    	//get the request and para
        $phrase=$this->_getParam('term');
        $model_ac= new Model_ACSearch();
        $limit=$this->_HAC_RETURN_LIMIT;
        
        //find venue name and id by phrase.
        $ac_results=$model_ac->findVenuesByPartial($phrase,$limit);
        $r=array();
        foreach ( $ac_results as $s){
            $t=array (
                "name"=>$s->getVenue_name(),
                "id"=>$s->getVenue_id()
            );
            $r[]=$t;
        }
        //return view with json.
        $this->_helper->json($r);
    }
    
    public function searchusersAction(){
    	
    	$name = $_REQUEST['ac'];
    	$userMapper = new Model_UsersMapper();
    	//find user list by name.
    	$users = $userMapper->findByPartial($name, $this->_HAC_RETURN_LIMIT);
    	$r=array();
    	foreach ($users as $user){
    		$t=array (
                "name"=>$user->getName(),
                "id"=>$user->getUser_id()
            );
            $r[]=$t;
    	}
    	//return user list to view.
    	$this->_helper->json($r);
    }
}

